package com.woniu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.Vacate;
import com.woniu.entity.vo.PageResponseVo;
import com.woniu.mapper.VacateMapper;
import com.woniu.service.IVacateService;
import com.woniu.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hyl
 * @since 2022-08-28
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VacateServiceImpl extends ServiceImpl<VacateMapper, Vacate> implements IVacateService {
    @Autowired
    private VacateMapper vacateMapper;

    @Override
    public void create(Vacate vacate) {
        vacateMapper.insert(vacate);
    }

    @Override
    public void approvalPass(Vacate vacate) {
        // 更新旧的审批流程
        Vacate v = vacateMapper.selectById(vacate.getVaId());
        v.setVaConfTime(vacate.getVaConfTime());
        v.setVaConfReason(vacate.getVaConfReason());
        v.setVaStatus("y");
        v.setConfUserId(vacate.getConfUserId());
        v.setVaEndTime(vacate.getVaConfTime());
        vacateMapper.update(v);

        // 向下一个审批人提交新的流程
        vacate.setVaId(null);
        vacate.setConfUserId(vacate.getNextConfUserId());
        vacate.setVaReason(null);
        vacate.setVaStartTime(vacate.getVaEndTime());
        vacate.setVaConfTime(null);
        vacate.setVaConfReason(null);
        vacateMapper.insert(vacate);
    }

    @Override
    public void approvalFail(Vacate vacate) {
        // 更新旧的审批流程
        Vacate v = vacateMapper.selectById(vacate.getVaId());
        v.setVaConfTime(vacate.getVaConfTime());
        v.setVaConfReason(vacate.getVaConfReason());
        v.setVaStatus("n");
        v.setConfUserId(vacate.getConfUserId());
        v.setVaEndTime(vacate.getVaConfTime());
        vacateMapper.update(v);

    }

    @Override
    public PageResponseVo<Vacate> getVacatePageByTerms(Integer page, Integer count, Vacate vacate) {
        Page<Vacate> pager = new Page<>(page, count);
        QueryWrapper<Vacate> queryWrapper = new QueryWrapper<>();
        if (EmptyUtil.isEmpty(vacate)){
            queryWrapper.eq("1","1");
        }
        if (EmptyUtil.isNoEmpty(vacate.getUserId())) {
            queryWrapper.eq("u1.user_id", vacate.getUserId());
        }
        if (EmptyUtil.isNoEmpty(vacate.getConfUserId())) {
            queryWrapper.eq("u2.conf_user_id", vacate.getConfUserId());
        }
        if (EmptyUtil.isNoEmpty(vacate.getVaStatus())) {
            queryWrapper.eq("va_status", vacate.getVaStatus());
        }else{
            queryWrapper.isNull("va_status");
        }
        if (EmptyUtil.isNoEmpty(vacate.getVaStartTime()) &&EmptyUtil.isNoEmpty(vacate.getVaEndTime())) {
            queryWrapper.between("va_start_time", vacate.getVaStartTime(), vacate.getVaEndTime());
        }
        if(EmptyUtil.isNoEmpty(vacate.getDept())){
            queryWrapper.eq("d1.dept_id",vacate.getDept().getDeptId());
        }
        IPage<Vacate> paging = vacateMapper.getVacateWithPage(pager, queryWrapper);
        return new PageResponseVo<>(paging.getTotal(), paging.getRecords(), paging.getCurrent(), paging.getSize());
    }

    @Override
    public Vacate queryVacateDetails(Integer id) {
        return vacateMapper.selectById(id);
    }
}
